﻿Imports System
Imports IDServer
Imports System.Threading.Thread
Imports System.IO
Imports System.String
Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient

Public Class Frm_SelectBO
	Implements IDServer.IDServer.IDServer

	Public ActionHelp As IDServer.IDServer.ActionBtn = IDServer.IDServer.ActionBtn.ActionHelp
    Private mDataset As DataSet
    Private FrmMain As New Frm_MainForm
	Public Fields, strFileName As String
    Private WithEvents MainFrom As New Frm_MainForm
    Private Captionstr As String = "付款條件^Payterm&幣別^Currency&訂購單號^PONum&採購項次^POSN&入庫單^BONum&條碼^Barcode" & _
                                    "&代碼^Code&中文貨品名^Cnname&英文貨品名^Enname&顔色^Color&規格^Spec&單位^Unit" & _
                                    "&送貨數量^GoodsAmount&剩餘數量^OverPlus&單價^Price&數量^Qty" & _
                                    "&折扣^Discount&貨款^Payment&稅額^Tax&總金額^TAmount&折扣額^DisAmount&金額^Amount"
    Private Captionstr1 As String = "訂購單號^PONum&採購項次^POSN&費用^Charge&訂單金額^TAmount&餘額^Overplus&備註^Remark"


#Region "inteface define"

    Public Function ActionAdd() As Boolean Implements IDServer.IDServer.IDServer.ActionAdd

    End Function

    Public Function ActionCancel() As Boolean Implements IDServer.IDServer.IDServer.ActionCancel

    End Function

    Public Function ActionDelete() As Boolean Implements IDServer.IDServer.IDServer.ActionDelete

    End Function

    Public Function ActionEdit() As Boolean Implements IDServer.IDServer.IDServer.ActionEdit

    End Function

    Public Function ActionExport() As Boolean Implements IDServer.IDServer.IDServer.ActionExport

    End Function

    Public Function ActionFilter() As Boolean Implements IDServer.IDServer.IDServer.ActionFilter

    End Function

    Public Function ActionGoFirst() As Boolean Implements IDServer.IDServer.IDServer.ActionGoFirst

    End Function

    Public Function ActionGoLast() As Boolean Implements IDServer.IDServer.IDServer.ActionGoLast

    End Function

    Public Function ActionGoNext() As Boolean Implements IDServer.IDServer.IDServer.ActionGoNext

    End Function

    Public Function ActionGoPrevious() As Boolean Implements IDServer.IDServer.IDServer.ActionGoPrevious

    End Function

    Public Function ActionImport() As Boolean Implements IDServer.IDServer.IDServer.ActionImport

    End Function

    Public Function ActionInitialize() As Boolean Implements IDServer.IDServer.IDServer.ActionInitialize

    End Function

    Public Function ActionPrint() As Boolean Implements IDServer.IDServer.IDServer.ActionPrint

    End Function

    Public Function ActionSave() As Boolean Implements IDServer.IDServer.IDServer.ActionSave

    End Function

    Public Function ActionSelect() As Boolean Implements IDServer.IDServer.IDServer.ActionSelect

    End Function
#End Region

#Region "Windows Sub"
    Private Sub Frm_SelectGoods_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Try
            If e.KeyCode = Keys.Shift Then
                e.Handled = True
                If e.KeyCode = Keys.Enter Then
                    SendKeys.Send("+{TAB}")
                    SendKeys.Send("{F4}")
                End If
            Else
                Select Case e.KeyCode
                    Case Keys.Enter
                        e.Handled = True
                        SendKeys.Send("{TAB}")
                End Select
            End If
        Catch ex As Exception
            MsgBox("txtCode_KeyDown:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub Frm_SelectGoods_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Me.Cursor = Cursors.WaitCursor

            DataBindGridView(Me.dgrd1, Captionstr)
            DataBindGrid(Me.dgrd1, Captionstr)
            'BindGridCaption(Me.dgrdCustomer, BindHashTable(Captionstr))
            FormatDataGridView(Me.dgrd1)
            With Me.dgrd1
                .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize
                .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
                .MultiSelect = PubMultiSelect
            End With

            DataBindGridView(Me.dgrd2, Captionstr1)
            DataBindGrid(Me.dgrd2, Captionstr1)
            'BindGridCaption(Me.dgrdCustomer, BindHashTable(Captionstr))
            FormatDataGridView(Me.dgrd2)
            With Me.dgrd2
                .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize
                .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
                .MultiSelect = PubMultiSelect
            End With

       
            Me.txtBarcode.Focus()


            'listAllControl()

        Catch ex As Exception
            Me.Cursor = Cursors.Default
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub TabControl1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TabControl1.KeyDown
        Try
            If e.KeyCode = Keys.Space Then
                If mDataset Is Nothing Then
                    Exit Sub
                End If

                If mDataset.Tables.Count = 0 Then
                    Exit Sub
                End If

                If Me.TabControl1.SelectedIndex = 0 Then
                    'Dgrd1
                    If mDataset.Tables(0).Rows.Count = 0 Then
                        Exit Sub
                    End If

                    If dgrd1.SelectedRows.Count = 0 Then
                        Exit Sub
                    End If

                    Dim CheckValue, strCheck As String

                    CheckValue = dgrd1.SelectedRows(0).Cells("Check").Value.ToString
                    If CheckValue = "True" Then
                        strCheck = "False"
                    Else
                        strCheck = "True"
                    End If

                    For I As Integer = 0 To dgrd1.SelectedRows.Count - 1
                        dgrd1.SelectedRows(I).Cells("Check").Value = strCheck
                    Next

                Else
                    'Dgrd2
                    If mDataset.Tables(1).Rows.Count = 0 Then
                        Exit Sub
                    End If

                    If dgrd2.SelectedRows.Count = 0 Then
                        Exit Sub
                    End If


                    Dim CheckValue, strCheck As String
                    CheckValue = dgrd2.SelectedRows(0).Cells("Check").Value.ToString
                    If CheckValue = "True" Then
                        strCheck = "False"
                    Else
                        strCheck = "True"
                    End If

                    For I As Integer = 0 To dgrd2.SelectedRows.Count - 1
                        dgrd2.SelectedRows(I).Cells("Check").Value = strCheck
                    Next


                End If
            ElseIf e.KeyCode = Keys.Enter Then
                Me.btnOK_Click(sender, e)
            End If
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub txtBarcode_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtBarcode.KeyDown, txtCode.KeyDown, _
                                   txtCnname.KeyDown, txtEnname.KeyDown, _
                                     txtBONum.KeyDown, txtPONum.KeyDown
        Try
            If e.KeyCode = Keys.Enter Then
                Select Case e.KeyCode
                    Case Keys.Enter
                        e.Handled = True
                        Me.BtnFind_Click(sender, e)
                    Case Keys.Escape
                        Me.btnCancel_Click(sender, e)
                End Select
            End If
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub txtBarcode_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBarcode.TextChanged, txtCnname.TextChanged, _
               txtCode.TextChanged, txtEnname.TextChanged, txtPONum.TextChanged, txtBONum.TextChanged
        Me.BtnFind_Click(sender, e)

    End Sub

    Private Sub BtnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnFind.Click

        Try
            Me.Cursor = Cursors.WaitCursor

            Dim Table(1) As String
            Dim sql(1) As String

            '供應商
            Table(0) = "BODetail" & vbCrLf
            sql(0) = "SELECT  convert(bit,0) as [Check], " & vbCrLf
            sql(0) += "          b.payterm," & vbCrLf
            sql(0) += "          b.currency," & vbCrLf
            sql(0) += "          a.ponum," & vbCrLf
            sql(0) += "          a.ponumitem AS posn," & vbCrLf
            sql(0) += "          a.bonum," & vbCrLf
            sql(0) += "          barcode = Replace(b.barcode,'*','')," & vbCrLf
            sql(0) += "          b.code," & vbCrLf
            sql(0) += "          b.cnname," & vbCrLf
            sql(0) += "          b.enname," & vbCrLf
            sql(0) += "          b.color," & vbCrLf
            sql(0) += "          b.spec," & vbCrLf
            sql(0) += "          b.unit," & vbCrLf
            sql(0) += "          a.goodsamount," & vbCrLf
            sql(0) += "          a.goodsamount - a.invqty AS overplus," & vbCrLf
            sql(0) += "          b.price," & vbCrLf
            sql(0) += "          b.discountb AS discount," & vbCrLf
            sql(0) += "          payment = a.goodsamount * b.price," & vbCrLf
            sql(0) += "          tax = b.taxrate / 100 * a.goodsamount * b.price," & vbCrLf
            sql(0) += "          tamount = (1 + b.taxrate / 100) * a.goodsamount * b.price," & vbCrLf
            sql(0) += "          disamount = b.discountb / 100 * ((1 + b.taxrate / 100) * a.goodsamount * b.price)," & vbCrLf
            sql(0) += "          amount = (1 - b.discountb / 100) * ((1 + b.taxrate / 100) * a.goodsamount * b.price)" & vbCrLf
            sql(0) += " FROM     (SELECT a.bonum," & vbCrLf
            sql(0) += "                  a.ponum," & vbCrLf
            sql(0) += "                  a.ponumitem," & vbCrLf
            sql(0) += "                  a.goodsbarcode," & vbCrLf
            sql(0) += "                  a.goodsamount," & vbCrLf
            sql(0) += "                  Isnull(b.invqty,0) invqty" & vbCrLf
            sql(0) += "           FROM   (SELECT a.bonum," & vbCrLf
            sql(0) += "                          b.ponum," & vbCrLf
            sql(0) += "                          b.ponumitem," & vbCrLf
            sql(0) += "                          b.goodsbarcode," & vbCrLf
            sql(0) += "                          b.goodsamount" & vbCrLf
            sql(0) += "                   FROM   stor_trademaster a," & vbCrLf
            sql(0) += "                          stor_tradedetail b" & vbCrLf
            sql(0) += "                   WHERE  a.tradetype = '+'" & vbCrLf
            sql(0) += "                          AND a.inorouttype = '02'" & vbCrLf
            sql(0) += "                          AND a.bostatus = 'B'" & vbCrLf
            sql(0) += "                          AND a.bonum = b.bonum) a" & vbCrLf
            sql(0) += "                  LEFT JOIN (SELECT   d.ponum," & vbCrLf
            sql(0) += "                                      d.posn," & vbCrLf
            sql(0) += "                                      d.bonum," & vbCrLf
            sql(0) += "                                      d.barcode," & vbCrLf
            sql(0) += "                                      Sum(d.qty) AS invqty" & vbCrLf
            sql(0) += "                             FROM     poinvoicea c," & vbCrLf
            sql(0) += "                                      poinvoiceb d" & vbCrLf
            sql(0) += "                             WHERE    c.billstatusid <> 'A'" & vbCrLf
            sql(0) += "                                      AND c.num = d.num" & vbCrLf
            sql(0) += "                             GROUP BY d.ponum," & vbCrLf
            sql(0) += "                                      d.posn," & vbCrLf
            sql(0) += "                                      d.bonum," & vbCrLf
            sql(0) += "                                      d.barcode) b" & vbCrLf
            sql(0) += "                    ON a.bonum = b.bonum" & vbCrLf
            sql(0) += "                       AND a.ponum = b.ponum" & vbCrLf
            sql(0) += "                       AND a.ponumitem = b.posn" & vbCrLf
            sql(0) += "                       AND a.goodsbarcode = b.barcode" & vbCrLf
            sql(0) += "           WHERE  a.goodsamount - Isnull(b.invqty,0) > 0) a," & vbCrLf
            sql(0) += "          (SELECT DISTINCT a.num," & vbCrLf
            sql(0) += "                           a.DATE," & vbCrLf
            sql(0) += "                           a.saleordeno," & vbCrLf
            sql(0) += "                           a.requestnum," & vbCrLf
            sql(0) += "                           suppliercode = CASE " & vbCrLf
            sql(0) += "                                            WHEN Isnull(e.cnname,'') = '' THEN e.enname" & vbCrLf
            sql(0) += "                                            WHEN e.cnname = '' THEN e.enname" & vbCrLf
            sql(0) += "                                            ELSE e.cnname" & vbCrLf
            sql(0) += "                                          END," & vbCrLf
            sql(0) += "                           a.taxrate," & vbCrLf
            sql(0) += "                           payterm = m.lista," & vbCrLf
            sql(0) += "                           currency = c.lista," & vbCrLf
            sql(0) += "                           a.discount," & vbCrLf
            sql(0) += "                           a.payment," & vbCrLf
            sql(0) += "                           a.tax," & vbCrLf
            sql(0) += "                           a.other," & vbCrLf
            sql(0) += "                           a.tamount," & vbCrLf
            sql(0) += "                           a.disamount," & vbCrLf
            sql(0) += "                           a.amount," & vbCrLf
            sql(0) += "                           billstatusid = f.lista," & vbCrLf
            sql(0) += "                           b.sn," & vbCrLf
            sql(0) += "                           b.barcode," & vbCrLf
            sql(0) += "                           g.code," & vbCrLf
            sql(0) += "                           g.cnname," & vbCrLf
            sql(0) += "                           g.enname," & vbCrLf
            sql(0) += "                           color = h.colorcname," & vbCrLf
            sql(0) += "                           spec = j.sizesname," & vbCrLf
            sql(0) += "                           unit = i.lista," & vbCrLf
            sql(0) += "                           b.price," & vbCrLf
            sql(0) += "                           b.qty," & vbCrLf
            sql(0) += "                           b.discount AS discountb," & vbCrLf
            sql(0) += "                           b.payment AS paymentb," & vbCrLf
            sql(0) += "                           b.tax AS taxb," & vbCrLf
            sql(0) += "                           b.tamount AS tamountb," & vbCrLf
            sql(0) += "                           b.disamount AS disamountb," & vbCrLf
            sql(0) += "                           b.amount AS amountb" & vbCrLf
            sql(0) += "           FROM   pur_poa a," & vbCrLf
            sql(0) += "                  pur_pob b," & vbCrLf
            sql(0) += "                  (SELECT listcode," & vbCrLf
            sql(0) += "                          lista" & vbCrLf
            sql(0) += "                   FROM   dbo.bsc_otherdefineb" & vbCrLf
            sql(0) += "                   WHERE  itemno = (SELECT Top 1  id" & vbCrLf
            sql(0) += "                                    FROM   dbo.bsc_otherdefinea" & vbCrLf
            sql(0) += "                                    WHERE  itemcode = 'CurrencyDefine')) c," & vbCrLf
            sql(0) += "                  bsc_dept d," & vbCrLf
            sql(0) += "                  bsc_supplier e," & vbCrLf
            sql(0) += "                  (SELECT listcode," & vbCrLf
            sql(0) += "                          lista" & vbCrLf
            sql(0) += "                   FROM   dbo.bsc_otherdefineb" & vbCrLf
            sql(0) += "                   WHERE  itemno = (SELECT Top 1  id" & vbCrLf
            sql(0) += "                                    FROM   dbo.bsc_otherdefinea" & vbCrLf
            sql(0) += "                                    WHERE  itemcode = 'BillstatusDefine')) f," & vbCrLf
            sql(0) += "                  bsc_goods g," & vbCrLf
            sql(0) += "                  bsc_colordefine h," & vbCrLf
            sql(0) += "                  (SELECT ob.listcode," & vbCrLf
            sql(0) += "                          ob.lista" & vbCrLf
            sql(0) += "                   FROM   bsc_otherdefinea oa," & vbCrLf
            sql(0) += "                          bsc_otherdefineb ob" & vbCrLf
            sql(0) += "                   WHERE  oa.itemcode = 'UnitDefin'" & vbCrLf
            sql(0) += "                          AND oa.id = ob.itemno) i," & vbCrLf
            sql(0) += "                  bsc_sizedefine j," & vbCrLf
            sql(0) += "                  (SELECT listcode," & vbCrLf
            sql(0) += "                          lista" & vbCrLf
            sql(0) += "                   FROM   dbo.bsc_otherdefineb" & vbCrLf
            sql(0) += "                   WHERE  itemno = (SELECT Top 1  id" & vbCrLf
            sql(0) += "                                    FROM   dbo.bsc_otherdefinea" & vbCrLf
            sql(0) += "                                    WHERE  itemcode = 'Incidental Charges')) l," & vbCrLf
            sql(0) += "                  (SELECT listcode," & vbCrLf
            sql(0) += "                          lista" & vbCrLf
            sql(0) += "                   FROM   dbo.bsc_otherdefineb" & vbCrLf
            sql(0) += "                   WHERE  itemno = (SELECT Top 1  id" & vbCrLf
            sql(0) += "                                    FROM   dbo.bsc_otherdefinea" & vbCrLf
            sql(0) += "                                    WHERE  itemcode = 'Payterm')) m" & vbCrLf
            sql(0) += "           WHERE  a.suppliercode = '" & PubSelectPriceSupplier & "'" & vbCrLf
            sql(0) += "                  AND (a.billstatusid = 'B'" & vbCrLf
            sql(0) += "                        OR a.billstatusid = 'F')" & vbCrLf
            sql(0) += "                  AND a.num = b.num" & vbCrLf
            sql(0) += "                  AND a.currency = c.listcode" & vbCrLf
            ''sql(0) += "                  AND a.dept = d.fcode" & vbCrLf
            sql(0) += "                  AND a.suppliercode = e.code" & vbCrLf
            sql(0) += "                  AND a.billstatusid = f.listcode" & vbCrLf
            sql(0) += "                  AND b.barcode = g.barcode" & vbCrLf
            sql(0) += "                  AND g.color = h.colorcode" & vbCrLf
            sql(0) += "                  AND g.unit = i.listcode" & vbCrLf
            sql(0) += "                  AND g.spec = j.sizecode" & vbCrLf
            sql(0) += "                  AND a.payterm = m.listcode) b" & vbCrLf
            sql(0) += " WHERE 1=1 "

            If Me.txtPONum.TextLength > 0 Then
                sql(0) += " and a.PoNum like '%" & Me.txtPONum.Text & "%'"
            End If


            If Me.txtBONum.TextLength > 0 Then
                sql(0) += " and a.BoNum like '%" & Me.txtBONum.Text & "%'"
            End If

            If Me.txtBarcode.TextLength > 0 Then
                sql(0) += " and b.Barcode like '%" & Me.txtBarcode.Text & "%'"
            End If

            If Me.txtCnname.TextLength > 0 Then
                sql(0) += " and b.Cnname like '%" & Me.txtCnname.Text & "%'"
            End If

            If Me.txtEnname.TextLength > 0 Then
                sql(0) += " and b.Enname like '%" & Me.txtEnname.Text & "%'"
            End If

            If Me.txtCode.TextLength > 0 Then
                sql(0) += " and b.Code like '%" & Me.txtCode.Text & "%'"
            End If

            sql(0) += "  AND   a.ponum = b.num        AND a.ponumitem = b.sn" & vbCrLf
            sql(0) += "          AND a.goodsbarcode = b.barcode" & vbCrLf
            sql(0) += " ORDER BY b.suppliercode," & vbCrLf
            sql(0) += "          a.bonum," & vbCrLf
            sql(0) += "          a.ponum," & vbCrLf
            sql(0) += "          a.ponumitem" & vbCrLf

            Table(1) = "Charge" & vbCrLf

            sql(1) = "SELECT  convert(bit,0) as [Check],  a.num AS ponum," & vbCrLf
            sql(1) += "          a.sn AS posn," & vbCrLf
            sql(1) += "          dbo.Getother('C','Incidental Charges',a.charge) charge," & vbCrLf
            sql(1) += "          a.tamount," & vbCrLf
            sql(1) += "          a.tamount - Sum(Isnull(b.amount,0)) AS overplus," & vbCrLf
            sql(1) += "          remark = Isnull(b.remark,'')" & vbCrLf
            sql(1) += " FROM     (SELECT b.num," & vbCrLf
            sql(1) += "                  b.sn," & vbCrLf
            sql(1) += "                  b.charge," & vbCrLf
            sql(1) += "                  b.amount AS tamount" & vbCrLf
            sql(1) += "           FROM   pur_poa a," & vbCrLf
            sql(1) += "                  pur_poc b" & vbCrLf
            sql(1) += "           WHERE  (a.suppliercode = '" & PubSelectPriceSupplier & "'" & vbCrLf
            sql(1) += "                   AND a.billstatusid = 'B'" & vbCrLf
            sql(1) += "                    OR a.billstatusid = 'F')" & vbCrLf
            sql(1) += "                  AND a.num = b.num) a" & vbCrLf
            sql(1) += "          LEFT JOIN (SELECT b.*" & vbCrLf
            sql(1) += "                     FROM   poinvoicea a," & vbCrLf
            sql(1) += "                            poinvoicec b" & vbCrLf
            sql(1) += "                     WHERE  a.billstatusid <> 'A'" & vbCrLf
            sql(1) += "                            AND a.num = b.num) b" & vbCrLf
            sql(1) += "            ON a.num = b.ponum" & vbCrLf
            sql(1) += "               AND a.sn = b.posn WHERE 1=1 " & vbCrLf

            If Me.txtPONum.TextLength > 0 Then
                sql(1) += " and a.num like '%" & Me.txtPONum.Text & "%'"
            End If

            sql(1) += " GROUP BY a.num," & vbCrLf
            sql(1) += "          a.sn," & vbCrLf
            sql(1) += "          a.charge," & vbCrLf
            sql(1) += "          a.tamount," & vbCrLf
            sql(1) += "          b.remark  " & vbCrLf
            sql(1) += " HAVING   a.tamount - Sum(Isnull(b.amount,0)) > 0" & vbCrLf
            sql(1) += " ORDER BY a.num," & vbCrLf
            sql(1) += "          a.sn" & vbCrLf

            mDataset = GetDataSet(sql, Table)
            Me.dgrd1.DataSource = mDataset.Tables(0)
            Me.dgrd2.DataSource = mDataset.Tables(1)

            dgrd1.ReadOnly = False
            For Each col As DataGridViewColumn In dgrd1.Columns
                If col.Name <> "Check" Then
                    col.ReadOnly = True
                End If
            Next

            dgrd2.ReadOnly = False
            For Each col As DataGridViewColumn In dgrd2.Columns
                If col.Name <> "Check" Then
                    col.ReadOnly = True
                End If
            Next

            dgrd1.Columns("Check").HeaderText = "選擇"
            dgrd1.Columns("Check").Width = 40
            dgrd1.Columns("Check").DisplayIndex = 0

            dgrd2.Columns("Check").HeaderText = "選擇"
            dgrd2.Columns("Check").Width = 40
            dgrd2.Columns("Check").DisplayIndex = 0
        


        Catch ex As Exception
            Me.Cursor = Cursors.Default
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            Me.Cursor = Cursors.Default

        End Try

    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Try
            If Me.dgrd1.SelectedRows.Count > 0 Or dgrd2.SelectedRows.Count > 0 Then
                PubGoodsDataSet.Tables.Clear()

                Dim tb As New DataTable
                Dim tb1 As New DataTable


                '將Dgrd1選中的行添加到PubGoodsDataSet
                tb = mDataset.Tables(0).Copy
                'tb.Rows.Clear()

                For I As Integer = dgrd1.Rows.Count - 1 To 0 Step -1
                    Dim row As DataRow
                    If dgrd1.Rows(I).Cells("Check").Value = "False" Then
                        tb.Rows.RemoveAt(I)
                    End If
                Next

                PubGoodsDataSet.Tables.Add(tb)

                tb1 = mDataset.Tables(1).Copy

                For I As Integer = dgrd2.Rows.Count - 1 To 0 Step -1
                    Dim row As DataRow
                    If dgrd2.Rows(I).Cells("Check").Value = "False" Then
                        tb1.Rows.RemoveAt(I)
                    End If
                Next

                PubGoodsDataSet.Tables.Add(tb1)

                PubGoodsDataSet.AcceptChanges()



            End If

        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)

        Finally
            Me.Dispose()


        End Try

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        'Me.Close()
        Me.Dispose()


    End Sub

    Private Sub dgrdGoods_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgrd1.CellDoubleClick
        Me.btnOK_Click(sender, e)
    End Sub

    Private Sub dgrdGoods_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgrd1.KeyDown
        Try
            If e.KeyCode = Keys.Enter Then
                Select Case e.KeyCode
                    Case Keys.Enter
                        e.Handled = True
                        Me.btnOK_Click(sender, e)
                    Case Keys.Escape
                        Me.btnCancel_Click(sender, e)
                End Select
            End If
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

    Private Sub dgrdGoods_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgrd1.CellContentClick

    End Sub

    Private Sub btnSelAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelAll.Click
        Try
            If mDataset Is Nothing Then
                Exit Sub
            End If

            If mDataset.Tables.Count = 0 Then
                Exit Sub
            End If

            If mDataset.Tables(0).Rows.Count = 0 Then
                Exit Sub
            End If

            '選擇Dgrd1
            For I As Integer = 0 To dgrd1.Rows.Count - 1
                dgrd1.Rows(I).Cells("Check").Value = "True"
            Next

            If mDataset.Tables(1).Rows.Count = 0 Then
                Exit Sub
            End If

            '選擇Dgrd12
            For I As Integer = 0 To dgrd2.Rows.Count - 1
                dgrd2.Rows(I).Cells("Check").Value = "True"
            Next

        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub btnClearSel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearSel.Click
        Try
            If mDataset Is Nothing Then
                Exit Sub
            End If

            If mDataset.Tables.Count = 0 Then
                Exit Sub
            End If

            If mDataset.Tables(0).Rows.Count = 0 Then
                Exit Sub
            End If

            '選擇Dgrd1
            For I As Integer = 0 To dgrd1.Rows.Count - 1
                dgrd1.Rows(I).Cells("Check").Value = "False"
            Next

            If mDataset.Tables(1).Rows.Count = 0 Then
                Exit Sub
            End If

            '選擇Dgrd12
            For I As Integer = 0 To dgrd2.Rows.Count - 1
                dgrd2.Rows(I).Cells("Check").Value = "False"
            Next

        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

#End Region

#Region "Function Define"


#End Region


   

    Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint

    End Sub
End Class